# -*- coding: utf-8 -*-
import scrapy
from parsel import Selector
from cnblogsSpider.items import CnblogsspiderItem

class CnblogsSpider(scrapy.Spider):
    name = 'cnblogs'
    allowed_domains = ['cnblogs.com']
    start_urls = ['https://www.cnblogs.com/qiyeboy']

    def parse(self, response):
        pagers = response.xpath('.//*[@class="day"]')
        for pager in pagers:
            url = pager.xpath('.//*[@class="postTitle"]/a/@href').extract()[0]
            title = pager.xpath('.//*[@class="postTitle"]/a/text()').extract()[0]
            time = pager.xpath('.//*[@class="dayTitle"]/a/text()').extract()[0]
            content = pager.xpath('.//*[@class="postCon"]/div/text()').extract()[0]
            # print url, title, time, content
            item = CnblogsspiderItem(url=url, time=time, title=title, content=content)
            yield item
        next_page = Selector(response).re(u'<a href="(/S*)">下一页</a>')
        if next_page:
            yield scrapy.Request(url=next_page[0], callback=self.parse)